package Game.Players;

import Game.Players.Units.*;

import java.util.Scanner;

public class Player {
   // public UnitIF creature;
    public Unit creature;

    public void selectUnit() {
        do {
            Scanner scan = new Scanner(System.in);
            String unitName = scan.nextLine();
            try {//LI
                TLightInfantry t = TLightInfantry.valueOf(unitName);
                creature = LightInfantry.make(t);
            } catch (IllegalArgumentException e_1) {
                try {//HI
                    THeavyInfantry t = THeavyInfantry.valueOf(unitName);
                    creature = HeavyInfantry.make(t);
                } catch (IllegalArgumentException e_2) {
                    try {//SM
                        TScoutMachines t = TScoutMachines.valueOf(unitName);
                        creature = ScoutMachines.make(t);
                    } catch (IllegalArgumentException e_3) {
                        try {//LM
                            TLightMachines t = TLightMachines.valueOf(unitName);
                            creature = LightMachines.make(t);
                        } catch (IllegalArgumentException e_4) {
                            try {//HM
                                THeavyMachines t = THeavyMachines.valueOf(unitName);
                                creature = HeavyMachines.make(t);
                            } catch (IllegalArgumentException e_5) {
                                try {//FF
                                    TFightingFortress t = TFightingFortress.valueOf(unitName);
                                    creature = FightingFortress.make(t);
                                } catch (IllegalArgumentException e_6) {
                                    System.out.println("Wrong unit name!");
                                }
                            }
                        }
                    }
                }
            }
        } while (creature.getHealth() == 0);
    }

/*
    public static void attack(Unit attacker, Unit target) {
        int damage = 0;
        boolean err = true;
        System.out.println("Select attack type: Range/Close");
        while(err) {
            Scanner scan = new Scanner(System.in);
            String attackType = scan.nextLine();
            if (attackType.equals("Range") && attacker.getAmmo() != 0) {
                err = false;
                System.out.println(attackType + " attack type selected");
                int shots = (attacker.getShots() > attacker.getAmmo() ? attacker.getAmmo() : attacker.getShots());//(a > b ? b : a) - found in net analog for min()
                damage = ((attacker.getRangeAttack() - target.getArmor()) * shots * attacker.getSquadSize());
                damage = (damage < 0 ? 0 : damage);
                attacker.setAmmo(attacker.getAmmo() - shots);//waiting for method refactoring to decreaseAmmo(int)
                target.decreaseArmor(shots * attacker.getSquadSize());//armor decr
                System.out.println(attacker.getName() + " deals " + damage + " damage to " + target.getName());
                System.out.println(attacker.getName() + " has " + attacker.getAmmo() * attacker.getSquadSize() + " shots left");
            } else if (attackType.equals("Close")) {
                err = false;
                System.out.println(attackType + " attack type selected");
                damage = ((attacker.getCloseAttack() - target.getArmor()) * attacker.getHits() * attacker.getSquadSize());
                damage = (damage < 0 ? 0 : damage);
                target.setArmor(target.getArmor() - attacker.getHits() * attacker.getSquadSize());//armor decr
                System.out.println(attacker.getName() + " deals " + damage + " damage to " + target.getName());
            } else {
                System.out.println("Something went wrong. Have another try");
            }
        }

        int hpLeft = target.getHealth() * target.getSquadSize() - damage;
        if (hpLeft > 0) {
            int sqLeft = (hpLeft - 1) / target.getHealth() + 1;
            target.setHealth(hpLeft / sqLeft); //target.setCurrentHealth(target.getCurrentHealth()+target.getHPRecovery() * sqLeft);
            target.setSquadSize(sqLeft);
            System.out.println("Enemy " + target.getName() + " health is: " + target.getHealth() + " x" + sqLeft + " =" + target.getHealth()*sqLeft);
        } else {
            System.out.println("Enemy " + target.getName() + " is dead");
            target.setHealth(0);
        }

    }
*/

    public static int createSquad(int delta) {
        int[] allowed = {1, 2, 5, 8, 12, 16};// limits of squad members amount, delta corresponds to index
        boolean first = true;
        int sqSize=1000;
        while (sqSize > allowed[delta]) {
            if(first) {
                System.out.println("Choose the number of squad members. Up to " + allowed[delta] + " units");
                first=false;
            } else {
                System.out.println("Too many squad members. Input a number under " + allowed[delta]);
            }
            Scanner squadNum = new Scanner(System.in);
            sqSize = squadNum.nextInt();
        }
        System.out.println(sqSize + " squad members selected");
        return sqSize;
    }

}

